package org.martyn.danbodroid.database;

import java.util.ArrayList;

import org.martyn.danbooru.DanbooruConnectSettings;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ServersTable
{
	public static final String TABLE_NAME = "servers";

	public static final String COLUMN_NAME_ID = "id";
	public static final String COLUMN_NAME_NAME = "name";
	public static final String COLUMN_NAME_ADDRESS = "address";
	public static final String COLUMN_NAME_PARSETYPE = "parseType";

	//private DatabaseHelper databaseHelper;

	
	public static ArrayList<DanbooruConnectSettings> getAll(DatabaseHelper databaseHelper)
	{
		SQLiteDatabase database = databaseHelper.getReadableDatabase();

		Cursor cursor = database.query(TABLE_NAME, new String[] {
				COLUMN_NAME_ID, COLUMN_NAME_NAME, COLUMN_NAME_ADDRESS,
				COLUMN_NAME_PARSETYPE }, null, null, null, null, COLUMN_NAME_ID
				+ " ASC");

		ArrayList<DanbooruConnectSettings> danbooruConnectSettingsList = new ArrayList<DanbooruConnectSettings>();

		while (cursor.moveToNext())
		{
			danbooruConnectSettingsList.add(new DanbooruConnectSettings(cursor
					.getInt(0), cursor.getString(1), cursor.getString(2),
					cursor.getString(3)));
		}

		if (cursor != null && !cursor.isClosed())
		{
			cursor.close();
		}

		DatabaseHelper.closeDatabase(database);

		return danbooruConnectSettingsList;
	}

	public static DanbooruConnectSettings get(DatabaseHelper databaseHelper, int serverId)
	{
		SQLiteDatabase database = databaseHelper.getReadableDatabase();

		Cursor cursor = database.query(TABLE_NAME, new String[] {
				COLUMN_NAME_ID, COLUMN_NAME_NAME, COLUMN_NAME_ADDRESS,
				COLUMN_NAME_PARSETYPE }, COLUMN_NAME_ID + " = " + serverId,
				null, null, null, COLUMN_NAME_ID + " ASC");

		DanbooruConnectSettings danbooruConnectSettings = null;

		if (cursor.moveToFirst())
		{
			danbooruConnectSettings = new DanbooruConnectSettings(
					cursor.getInt(0), cursor.getString(1), cursor.getString(2),
					cursor.getString(3));
		}

		if (cursor != null && !cursor.isClosed())
		{
			cursor.close();
		}

		DatabaseHelper.closeDatabase(database);

		return danbooruConnectSettings;
	}

	public static int insert(DatabaseHelper databaseHelper,
			String serverName,
			String serverAddress,
			String serverParseType)
	{
		SQLiteDatabase database = databaseHelper.getWritableDatabase();

		ContentValues values = new ContentValues();

		values.put(COLUMN_NAME_NAME, serverName);
		values.put(COLUMN_NAME_ADDRESS, serverAddress);
		values.put(COLUMN_NAME_PARSETYPE, serverParseType);

		long rowId = database.insert(TABLE_NAME, null, values);

		DatabaseHelper.closeDatabase(database);

		if (rowId > 0)
		{
			return (int) rowId;
		}

		throw new SQLException("Failed to insert row");
	}

	public static int update(DatabaseHelper databaseHelper,
			int serverId,
			String serverName,
			String serverAddress,
			String serverParseType)
	{
		SQLiteDatabase database = databaseHelper.getWritableDatabase();

		ContentValues values = new ContentValues();

		values.put(COLUMN_NAME_NAME, serverName);
		values.put(COLUMN_NAME_ADDRESS, serverAddress);
		values.put(COLUMN_NAME_PARSETYPE, serverParseType);

		int rowCount = database.update(TABLE_NAME, values, COLUMN_NAME_ID
				+ " = " + serverId, null);

		return rowCount;
	}

	public static int delete(DatabaseHelper databaseHelper, int serverId)
	{
		SQLiteDatabase database = databaseHelper.getWritableDatabase();

		int rowCount = database.delete(TABLE_NAME, COLUMN_NAME_ID + " = "
				+ serverId, null);

		DatabaseHelper.closeDatabase(database);

		return rowCount;
	}
}
